﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using cklib;
using Silmoon;
using Silmoon.Threading;

namespace Cooky.Client.RecordDir
{
    public partial class RecordForm : Silmoon.Windows.Forms.GenieForm
    {
        public string Domain;
        public int DomainID = 0;
        public string Host;
        public string RecordID = null;
        public Record[] RecordsCache = null;
        public Domain[] DomainsCache = null;
        public cklib.CookyDNSAPI cooky = null;
        public bool SubmitState = false;

        public RecordForm()
        {
            InitializeComponent();
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            panel1.Visible = false;
            base.OnFormClosing(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            drawForm();
            base.OnLoad(e);
        }
        void hideThis()
        {
            panel1.Visible = false;
            HideEx();
        }

        private void ctlCancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        void drawForm()
        {
            Threads.ExecAsync(delegate()
            {
                this.Invoke(new tmpHander(delegate()
                {
                    ctlNetworkComboBox.BeginUpdate();
                    ctlNetworkComboBox.Items.Clear();

                    for (int i = 0; i < cooky.AllServerNetmarks.Count; i++)
                        ctlNetworkComboBox.Items.Add(cooky.AllServerNetmarks[i]);

                    ctlNetworkComboBox.Text = "默认";
                    ctlNetworkComboBox.EndUpdate();

                    ctlValueComboBox.BeginUpdate();
                    ctlValueComboBox.Items.Clear();
                    foreach (Record item in RecordsCache)
                        ctlValueComboBox.Items.Add(item.Value);
                    ctlValueComboBox.EndUpdate();

                    ctlTypeComboBox.Text = "A";

                    if (RecordID != null)
                    {
                        Record record = cooky.GetRecord(RecordsCache, RecordID);
                        ctlHostTextBox.Text = Host;
                        ctlDomainTextBox.Text = Domain;
                        ctlTypeComboBox.Text = CookyDNSAPI.RecordTypeToString(record.Type);
                        ctlNetworkComboBox.Text = cooky.AllServerNetmarks[record.Netmark];
                        ctlValueComboBox.Text = record.Value;
                        ctlTTLMUD.Value = record.TTL;
                        ctlMXLevelMUD.Value = record.MXLevel;
                        if (ctlTypeComboBox.Text == "SOA")
                        {
                            ctlSubmitButton.Text = "不可用";
                            ctlSubmitButton.Enabled = false;
                        }
                        ctlValueComboBox.Focus();
                    }
                }));
            });
        }
        private void ctlMoreSwitchButton_OnSwitch(object sender, EventArgs e)
        {
            if (ctlMoreSwitchButton.SwitchState == Silmoon.Windows.Controls.SwitchButton.SwitchStateType.On)
                SetHeightEx(245, true);
            else
                SetHeightEx(176, true);
        }

        private void ctlSubmitButton_Click(object sender, EventArgs e)
        {
            if (ctlHostTextBox.Text == "") ctlHostTextBox.Text = "@";

            IPAddress ipbuff;
            if (!IPAddress.TryParse(ctlValueComboBox.Text, out ipbuff) && cklib.CookyDNSAPI.StringToRecordType(ctlTypeComboBox.Text) == DNS_TYPE.TYPE_A && MessageBox.Show("记录数据不是A记录所支持的，是否更改成CNAME别名记录？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) ctlTypeComboBox.Text = "CNAME";

            Record newRecord = new Record();
            newRecord.Subname = ctlHostTextBox.Text;
            newRecord.ID = RecordID;
            newRecord.Netmark = cooky.NoteToMark(ctlNetworkComboBox.Text);
            newRecord.MXLevel = (int)ctlMXLevelMUD.Value;
            newRecord.TTL = (int)ctlTTLMUD.Value;
            newRecord.Type = cklib.CookyDNSAPI.StringToRecordType(ctlTypeComboBox.Text);
            newRecord.Value = ctlValueComboBox.Text;

            if ((newRecord.Type == DNS_TYPE.TYPE_CNAME || newRecord.Type == DNS_TYPE.TYPE_MX || newRecord.Type == DNS_TYPE.TYPE_NS) && newRecord.Value.Substring(newRecord.Value.Length - 1, 1) != ".")
                newRecord.Value += ".";

            if (newRecord.Type == DNS_TYPE.TYPEx_URL && SmString.CutString(newRecord.Value, 7).ToLower() != "http://")
                newRecord.Value = "http://" + newRecord.Value;

            Threads.ExecAsync(delegate()
            {
                CookyResult result = null;
                if (RecordID != null)
                    result = cooky.ModifyRecord(newRecord, RecordID);
                else
                    result = cooky.CreateRecord(newRecord, Domain);
                this.Invoke(new tmpHander(delegate()
                {
                    if (!result.Success)
                    {
                        MessageBox.Show("错误：" + Translate.TranslateIt(result.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        SubmitState = true;
                        Close();
                    }
                }));
            }, null);
        }

        private void ctlTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ctlTypeComboBox.Text == "SOA")
            {
                ctlSubmitButton.Text = "不可用";
                ctlSubmitButton.Enabled = false;
            }
            else
            {
                ctlSubmitButton.Text = "提交";
                ctlSubmitButton.Enabled = true;
            }
        }
        private void RecordForm_Shown(object sender, EventArgs e)
        {
            FormGenieExtension.ShowControl(ctlCancelButton);
            FormGenieExtension.ShowControl(ctlSubmitButton, true, 200);
        }
    }
}
